package c.a.a.e0;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.os.Build.VERSION;
import java.io.Closeable;

/* compiled from: Utils */
public final class g {
    public static final ThreadLocal<PathMeasure> a = new a();
    public static final ThreadLocal<Path> b = new b();
    public static final ThreadLocal<Path> c = new c();
    public static final ThreadLocal<float[]> d = new d();
    public static final float e = ((float) (Math.sqrt(2.0d) / 2.0d));
    public static float f = -1.0f;

    /* compiled from: Utils */
    public class a extends ThreadLocal<PathMeasure> {
        public Object initialValue() {
            return new PathMeasure();
        }
    }

    /* compiled from: Utils */
    public class b extends ThreadLocal<Path> {
        public Object initialValue() {
            return new Path();
        }
    }

    /* compiled from: Utils */
    public class c extends ThreadLocal<Path> {
        public Object initialValue() {
            return new Path();
        }
    }

    /* compiled from: Utils */
    public class d extends ThreadLocal<float[]> {
        public Object initialValue() {
            return new float[4];
        }
    }

    public static void a(Path path, float f, float f2, float f3) {
        PathMeasure pathMeasure = (PathMeasure) a.get();
        Path path2 = (Path) b.get();
        Path path3 = (Path) c.get();
        pathMeasure.setPath(path, false);
        float length = pathMeasure.getLength();
        String str = "applyTrimPathIfNeeded";
        if (f == 1.0f && f2 == 0.0f) {
            c.a.a.c.a(str);
        } else if (length < 1.0f || ((double) Math.abs((f2 - f) - 1.0f)) < 0.01d) {
            c.a.a.c.a(str);
        } else {
            f *= length;
            f2 *= length;
            f3 *= length;
            float min = Math.min(f, f2) + f3;
            f = Math.max(f, f2) + f3;
            if (min >= length && f >= length) {
                min = (float) f.d(min, length);
                f = (float) f.d(f, length);
            }
            if (min < 0.0f) {
                min = (float) f.d(min, length);
            }
            if (f < 0.0f) {
                f = (float) f.d(f, length);
            }
            int i = (min > f ? 1 : (min == f ? 0 : -1));
            if (i == 0) {
                path.reset();
                c.a.a.c.a(str);
                return;
            }
            if (i >= 0) {
                min -= length;
            }
            path2.reset();
            pathMeasure.getSegment(min, f, path2, true);
            if (f > length) {
                path3.reset();
                pathMeasure.getSegment(0.0f, f % length, path3, true);
                path2.addPath(path3);
            } else if (min < 0.0f) {
                path3.reset();
                pathMeasure.getSegment(min + length, length, path3, true);
                path2.addPath(path3);
            }
            path.set(path2);
            c.a.a.c.a(str);
        }
    }

    public static void b(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception unused) {
            }
        }
    }

    public static float c() {
        if (f == -1.0f) {
            f = Resources.getSystem().getDisplayMetrics().density;
        }
        return f;
    }

    public static float d(Matrix matrix) {
        float[] fArr = (float[]) d.get();
        fArr[0] = 0.0f;
        fArr[1] = 0.0f;
        float f = e;
        fArr[2] = f;
        fArr[3] = f;
        matrix.mapPoints(fArr);
        return (float) Math.hypot((double) (fArr[2] - fArr[0]), (double) (fArr[3] - fArr[1]));
    }

    public static Bitmap e(Bitmap bitmap, int i, int i2) {
        if (bitmap.getWidth() == i && bitmap.getHeight() == i2) {
            return bitmap;
        }
        Bitmap createScaledBitmap = Bitmap.createScaledBitmap(bitmap, i, i2, true);
        bitmap.recycle();
        return createScaledBitmap;
    }

    public static void f(Canvas canvas, RectF rectF, Paint paint, int i) {
        if (VERSION.SDK_INT < 23) {
            canvas.saveLayer(rectF, paint, i);
        } else {
            canvas.saveLayer(rectF, paint);
        }
        c.a.a.c.a("Utils#saveLayer");
    }
}
